package com.boot.producer.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.boot.producer.entity.Violation;
import com.boot.producer.service.ViolationService;
import com.boot.producer.service.dto.ViolationTimeDto;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangZhilei
 * @since 2019-02-25
 */
@RestController
@RequestMapping("/violation")
@Api(value = "违规Controller", description = "违规Controller")
public class ViolationController {
    @Autowired
    ViolationService violationService;

    @GetMapping("list")
    @ApiOperation("获取所有")
    public List<Violation> list(){
        return violationService.selectList(null);
    }
    @GetMapping("selectTimesByDate")
    @ApiOperation("根据日期获取违规次数")
    public Map<Integer, Integer> listTimesByDate(@RequestParam("date")Date date){
        return violationService.listTimesByDate(date);
    }
    @GetMapping("selectViolationsByDate")
    @ApiOperation("根据日期获取违规列表")
    public List<Violation> listViolationsByDate(@RequestParam("date")Date date){
        return violationService.selectList(new EntityWrapper<Violation>().eq("date",date));
    }
    @PostMapping("save")
    @ApiOperation("插入一条")
    public Boolean save(@RequestBody Violation violation){
        int count = violationService.selectCount(new EntityWrapper<Violation>().eq("latitude", violation.getLatitude()).eq("longitude", violation.getLongitude()));
        if (count == 0){
            violationService.insert(violation);
            return true;
        } else {
            violationService.update(violation,new EntityWrapper<Violation>().eq("latitude", violation.getLatitude()).eq("longitude", violation.getLongitude()));
            return false;
        }
    }
    @ApiOperation("根据时间删除")
    @ResponseBody
    @GetMapping("/deleteByDate")
    public void deleteByDate(@RequestParam("date") Date date) {
        violationService.delete(new EntityWrapper<Violation>().eq("date",date));
    }
    @ApiOperation("获取一周分析数据")
    @GetMapping("/analysisData")
    @ResponseBody
    public Map<Integer, List<Integer>> getAnalysisData(@RequestParam("begin") Date begin,@RequestParam("end") Date end){
        return violationService.getAnalysisData(begin, end);
    }
    @ApiOperation("获取最近一周数据")
    @GetMapping("/weeklyData")
    @ResponseBody
    public Map<Integer, Integer> getWeeklyData(){
        return violationService.getWeeklyData();
    }
    @ApiOperation("根据范围获取数据")
    @GetMapping("/getByRange")
    @ResponseBody
    public Map<Integer, Integer> selectByRange(@RequestParam("begin") Date begin,@RequestParam("end") Date end){
        return violationService.getByRange(begin, end);
    }


}

